package com.heima.article.listen;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleConfigService;
import com.heima.common.constants.message.NewsUpOrDownConstants;
import com.heima.model.article.pojos.ApArticleConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
public class ArticleUpOrDownListener {

    @Autowired
    private ApArticleConfigService apArticleConfigService;

    @RabbitListener(queuesToDeclare = @Queue(NewsUpOrDownConstants.NEWS_UP_FOR_ARTICLE_CONFIG_QUEUE))
    public void handleNewsUp(String articleId){
        log.info("接收到上架消息内容 id为{} , 时间为： {}" ,articleId , LocalDateTime.now().toString());
        apArticleConfigService.update(Wrappers.<ApArticleConfig>lambdaUpdate().set(ApArticleConfig::getIsDown,false)
        .eq(ApArticleConfig::getArticleId,Long.valueOf(articleId)));

    }


    @RabbitListener(queuesToDeclare = @Queue(NewsUpOrDownConstants.NEWS_DOWN_FOR_ARTICLE_CONFIG_QUEUE))
    public void handleNewsDown(String articleId){
        log.info("接收到下架消息内容 id为{} , 时间为： {}" ,articleId , LocalDateTime.now().toString());
        apArticleConfigService.update(Wrappers.<ApArticleConfig>lambdaUpdate().set(ApArticleConfig::getIsDown,true)
                .eq(ApArticleConfig::getArticleId,Long.valueOf(articleId)));
        ApArticleConfig apArticleConfig = new ApArticleConfig();
        log.error("下架的状态为：{}" , apArticleConfig.getIsDown());
    }
}
